package com.medic.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.medic.entity.Assitant;
import com.medic.entity.Doctor;
import com.medic.entity.Pharmacist;
import com.medic.entity.User;
import com.medic.entity.group.pain.Plan;
import com.medic.entity.group.pain.Task;
import com.medic.service.impl.AssitantServiceImpl;
import com.medic.service.impl.DoctorServiceImpl;
import com.medic.service.impl.GroupServiceImpl;
import com.medic.service.impl.PharmacistServiceImpl;
import com.medic.service.impl.PlanServiceImpl;
import com.medic.service.impl.TaskServiceImpl;
import com.medic.service.impl.UserServiceImpl;
import com.medic.util.LoggerUtil;

@RequestMapping("web/info")
@Controller
public class InfoController {

	@Resource
	private AssitantServiceImpl assitantService;
	@Resource
	private DoctorServiceImpl doctorService;
	@Resource
	private PharmacistServiceImpl pharmacistService;
	@Resource
	private UserServiceImpl userService;
	
	@Resource
	private GroupServiceImpl groupService;
	@Resource
	private TaskServiceImpl taskService;
	@Resource
	private PlanServiceImpl planService;

	@RequestMapping("action/search")
	public String box() {

		return "web/searchbox";
	}

	@RequestMapping("/index")
	public String index() {

		return "web/INFO";
	}

	@ResponseBody
	@RequestMapping("action/data/{role}/{where}")
	public Object detail(@PathVariable("role") String role, @PathVariable("where") String where) {
		Map<String, Object> object = new HashMap<String, Object>();
		
		try {
			
			switch (role) {
			
			case "user":
				Page<User> udata= userService.getUser(1, where);
				object.put("rows",udata);
				object.put("total", udata.getPages());
				break;
			case "pharmacist":
				Page<Pharmacist> pdata= pharmacistService.getPharmacist(1, where);
				object.put("total", pdata.getPages());
				object.put("rows",pdata);
				break;
			case "doctor":
				Page<Doctor>  ddata=doctorService.getDoctor(1, where);
				object.put("rows",ddata);
				object.put("total",ddata.getPages());
				break;
			case "assitant":
				Page<Assitant> adata=assitantService.getAssitant(1,where);
				object.put("rows", adata);
				object.put("total", adata);
				break;

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return object;
	}

	@RequestMapping("/queryByUser")
	public String queryByPage(int userId, int current, int groupId, int status, Model model) {
		String result = "";
		long crDate = 0;
		try {
			if (status <= 1) {
				crDate = new Date().getTime();
			}
			Page<Task> tasks = taskService.getByPageUser(current, userId, groupId, status, crDate);

			model.addAttribute("tasks", tasks);
			model.addAttribute("pages", tasks.getPages());
			model.addAttribute("current", current);
			model.addAttribute("status", status);
			model.addAttribute("groupId", groupId);
			model.addAttribute("userId", userId);
			result = "/web/task";
		} catch (Exception e) {
			LoggerUtil.error("error : " + e.getMessage() + " - params [ userId : " + userId + ",groupId : " + groupId
					+ ",current : " + current + ",status : " + status + ",crDate : " + crDate + " ]");
			result = "error";
		}
		return result;
	}

	@RequestMapping("/queryPlan")
	public String query(int groupId, Model model) {
		String result = "";
		try {
			Plan plan = planService.getByGroupId(groupId);
			model.addAttribute("plan", plan);
			result = "/web/plan";
		} catch (Exception e) {
			LoggerUtil.error("error : " + e.getMessage() + " - params [ groupId : " + groupId + " ]");
			result = "error";
		}
		return result;
	}
	
	@RequestMapping("action/group/{role}/{roleid}")
	@ResponseBody
	public Object group(@PathVariable("role")String role,@PathVariable("roleid")int roleid) {
		Map<String,Object> object = new HashMap<String,Object>();
		try{
			switch(role){
			case "doctor":
				object.put("group", groupService.getGroupByDoctor(roleid));
				break;
			case "user":
				object.put("group", groupService.getGroupByUser(roleid));
				break;
			case "assitant":
				object.put("group", groupService.getGroupByAssitant(roleid));
				break;
			case "pharmacist":
				object.put("group", groupService.getGroupByPharmacist(roleid));
				break;
			}
			
			
		}catch(Exception ee){
			ee.printStackTrace();
		}
		
		
		return object;
	}
	
	@RequestMapping("remove/group/{role}/{groupid}/{roleid}")
	@ResponseBody
	public String remove(@PathVariable("groupid")int gid,@PathVariable("roleid")int rid,@PathVariable("role")String role ){
		
		try{
			switch(role){
			case "user":
				groupService.userExitGroup(gid,new int[]{rid});

				break;
			case "doctor":
				groupService.doctorExitGroup(gid,new int[]{rid});
				break;
			case "assitant":
				groupService.assitantExitGroup(gid,new int[]{rid});
				break;
			case "pharmacist":
				groupService.pharmacistExitGroup(gid,new int[]{rid});
				break;
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
			return "{\"status\":\"errror\"}";
		}
		return  "{\"status\":\"ok\"}";
	}
	
	
}
